package com.jiyinit.web.core.business.service;

import com.jiyinit.core.framework.base.paging.PagingResult;
import com.jiyinit.web.core.business.dao.TabSendMessageDao;
import com.jiyinit.web.core.business.dto.MessagePageRequest;
import com.jiyinit.web.core.business.dto.ReadMessageRequest;
import com.jiyinit.web.core.business.entity.TabSendMessageEntity;
import com.jiyinit.web.core.business.enums.CommonEnum;
import com.jiyinit.web.core.business.enums.SystemConstant;
import com.jiyinit.web.core.business.vo.SendMessageVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by lr on 2017/11/30.
 */
@Service("send.message")
public class ReadMessageServiceImpl {

    /**
     * 日志对象
     */
    private static Logger logger = LoggerFactory.getLogger(ReadMessageServiceImpl.class);

    @Resource(name ="send.tabSendMessageDao")
    private TabSendMessageDao tabSendMessageDao;

    public PagingResult<SendMessageVo> readByPage(MessagePageRequest request){
        request.setFunId(SystemConstant.NOTICE_BOARD);
        return tabSendMessageDao.readByPage(request);
    }


    public  int read(ReadMessageRequest readMessageRequest){
        return tabSendMessageDao.updateReadStatus(readMessageRequest);
    }


    public List<TabSendMessageEntity> findNoReadMessage(Integer userId){
        return tabSendMessageDao.findNoReadMessage(userId);
    }
    public void add(Integer userId,Integer[] alloterIds,Integer funId,Integer id){
        List<TabSendMessageEntity> mesList = new ArrayList();
        for( Integer uid: alloterIds){
            TabSendMessageEntity message = new TabSendMessageEntity();
            message.setTaskId(id);
            message.setFunId(funId);
            message.setReceiveUserId(new Integer(uid));
            message.setIsRead(CommonEnum.IsRead.NOREAD.getCode());
            message.setIsDelete(CommonEnum.IsDelete.COMMON.getCode());
            message.setCreateId(userId);
            message.setCreateTime(new Date());
            mesList.add(message);
        }
           if(mesList != null && mesList.size() >0) tabSendMessageDao.batchInsert(mesList);
    }
    public void add1(Integer userId,List<Integer> alloterIds,Integer funId,Integer id){
        List<TabSendMessageEntity> mesList = new ArrayList();
        for( Integer uid: alloterIds){
            TabSendMessageEntity message = new TabSendMessageEntity();
            message.setTaskId(id);
            message.setFunId(funId);
            message.setReceiveUserId(new Integer(uid));
            message.setIsRead(CommonEnum.IsRead.NOREAD.getCode());
            message.setIsDelete(CommonEnum.IsDelete.COMMON.getCode());
            message.setCreateId(userId);
            message.setCreateTime(new Date());
            mesList.add(message);
        }
        if(mesList != null && mesList.size() >0) tabSendMessageDao.batchInsert(mesList);
    }
}
